<?php
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/11
 * Time: 11:29
 */

namespace app\common\traits;
use app\common\message\model\SmsMessage;
use app\common\message\type\SmsType;
use app\common\model\Wesmslog;

trait SendMessage
{
    /**
     * 发送微信模板消息
     * @param $data ['phone'=>'xxx','param'=>['xx'=>'xxx'],'tempcode'=>'xxx']
     */
    public function sendSmss($data){
        $wechattype = new SmsType();
        $wechatmessage = new SmsMessage($wechattype,$data);
        $wesmslog = new Wesmslog();
        $str = '';
        foreach ($data as $key=>$v){
           if(is_array($v)){
                foreach ($v as $keys=>$vo){
                    $str.=$keys.":".$vo.';';
                }
           }else{
               $str.=$key.":".$v.';';
           }
        }
        $arr['send_id']     = $data['sendid'];
        $arr['receive_id']  = $data['phone'];
        $arr['order_id']    = $data['orderid'];
        $arr['log_type']    = 2;
        $arr['send_msg']    = $str;
        $array =  $wechatmessage->sendMessage();
        $result = true;
        $strs='';
        if(is_array($array['result']) && isset($array['result'])){
                if (!$array['result']['success']){
                    $arr['is_success']  = 2;
                    $arr['error_msg']   ='错误代码：'.$array['result']['err_code'];
                    $result = false;
                }
            }else{
                $arr['is_success']  = 2;
                foreach ($array as $key=>$v){
                    $strs.= $key.":".$v.';';
                }
                $arr['error_msg']   =$strs;
            $result = false;
        }
        $wesmslog->save($arr);
        return $result;
    }
}